$(document).ready(function() {
	$("#username").blur(function() {
		var username = $("#username").val();
		var patrn = /^[0-9]{11}$/;
		if(username != 0) {
			if(!patrn.test(username)) {
				$("#prompt").show();
				$("#prompt").text("手机号格式有误");
			} else {
				$("#prompt").hide();
			}
		} else {
			$("#prompt").text("手机号不能为空");
		}
	});
	$("#password").blur(function() {
		var password = $("#password").val();
		var patrn = /^[0-9a-zA-Z]{6,18}$/;
		if(password != 0) {
			if(!patrn.test(password)) {
				$("#cue").show();
				$("#cue").text("密码格式有误");
			} else {
				$("#cue").hide();
			}
		} else {
			$("#cue").text("密码不能为空");
		}
	});
	$("#verification").blur(function() {
		var verification = $("#verification").val();
		var patrn = /^[0-9a-zA-Z]{5}$/;
		if(verification != 0) {
			if(!patrn.test(verification)) {
				$("#hint").show();
				$("#hint").text("验证码格式有误");
			} else {
				var code = $("#verification").val();
				$.ajax({ //常用的就是ajax 也可以是get 和 post
					url: "/xiaomi/user/equalsrandomCode",
					type: "post",
					data: {
						"code": code
					},
					dataType: "json",
					success: function(data) {
						if(data.state == 1) {
							$("#hint").text("验证码错误");
							$("#codeimg").attr("src", "/xiaomi/user/randomCode?code=" + Math.random());
							return;
						} else if(data.state == 0) {
							$("#hint").hide();
							return;
						}
					}
				})
			}
		} else {
			$("#hint").text("验证码不能为空");
		}
	});

	$("#submit").click(function() {
		var telephone = $("#telephone").val();
		var password = $("#password").val();
		$.ajax({
			type: "post",
			url: "/xiaomi/user/login",
			data: {
				"telephone": telephone,
				"password": password,
			},
			dataType: "json",
			success: function(data) {
				console.info(data.msg);
				$("#hint").show();
				$("#hint").text(data.msg);
				if("登录成功" == data.msg) {
					window.location.href = "/xiaomi/index.html";
				}
			}
		});
	})
	$("#register").click(function() {
		var menus = $(".fr>ul>li>a");
		var menu1 = menus[0];
		var ids=$(menu1).attr("id");
		var id=ids.split("-")[1];
		console.info(id);
		$.ajax({
			type: "post",
			url: "/xiaomi/user/loginout",
			data: {
				"id":id
			},
			dataType: "json",
			success: function(data) {
				console.info(data.msg);
				if("退出成功" == data.msg) {
					window.location.href = "/xiaomi/index.html";
				}
			}
		});
	})
})